<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <a-tabs v-model="activeKey" @change="handleChangeTab">
      <a-tab-pane key="notGenerated" tab="未生成">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="loadNotGeneratedData">
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-item label="产品类型">
                  <a-radio-group v-model="productType" @change="loadNotGeneratedData">
                    <a-radio :value="1">粉类</a-radio>
                    <a-radio :value="2">液洗类</a-radio>
                    <a-radio :value="3">其他</a-radio>
                  </a-radio-group>
                </a-form-item>
              </a-col>
              <a-col :span="24" style="margin-top: 10px">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button @click="addSchedule" type="primary" icon="plus">添加</a-button>
                  <a-button @click="addPlan" type="primary" icon="plus" v-if="productType == 3" style="margin-left: 5px"
                    >新增</a-button
                  >
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <!-- table区域-begin -->
        <div v-if="productType == 3">
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            class="j-table-force-nowrap"
            :scroll="{ x: true }"
            :columns="planColumns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="handleEditPlan(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDeletePlan(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
          </a-table>
        </div>
        <div v-else>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            class="j-table-force-nowrap"
            :scroll="{ x: true }"
            :columns="dataColumns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
          </a-table>
        </div>
      </a-tab-pane>
      <a-tab-pane key="tempSave" tab="暂存中">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="tempSaveTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="tempSaveTable.dataSource"
          :pagination="{
            ...tempSaveTable.ipagination,
            onChange: handleTempSavePageChange, // 监听页码变化
            onShowSizeChange: handleTempSavePageSizeChange, // 监听每页数量变化
          }"
          :loading="tempSaveTable.loading"
          :rowSelection="{
            selectedRowKeys: tempSaveTable.selectedRowKeys,
            onChange: onScheduleSelectChangeTempSave,
            type: 'radio',
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>
            <act-apply-btn
              @success="loadData"
              :data-id="record.id"
              :serviceName="serviceName"
              :variables="record"
            ></act-apply-btn>
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
      <a-tab-pane key="needEdit" tab="需修改">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="needEditTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="needEditTable.dataSource"
          :pagination="{
            ...needEditTable.ipagination,
            onChange: handleNeedEditPageChange, // 监听页码变化
            onShowSizeChange: handleNeedEditPageSizeChange, // 监听每页数量变化
          }"
          :loading="needEditTable.loading"
          :rowSelection="{
            selectedRowKeys: needEditTable.selectedRowKeys,
            onChange: onScheduleSelectChangeNeedEdit,
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>
            <act-apply-btn
              @success="loadData"
              :data-id="record.id"
              :serviceName="serviceName"
              :variables="record"
              text="重新提交"
            ></act-apply-btn>
            <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
      <a-tab-pane key="approving" tab="审批中">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="approvingTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="approvingTable.dataSource"
          :pagination="{
            ...approvingTable.ipagination,
            onChange: handleApprovingPageChange, // 监听页码变化
            onShowSizeChange: handleApprovingPageSizeChange, // 监听每页数量变化
          }"
          :loading="approvingTable.loading"
          :rowSelection="{
            selectedRowKeys: approvingTable.selectedRowKeys,
            onChange: onScheduleSelectChangeApproving,
            type: 'radio',
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
            <a-divider type="vertical" />
            <a @click="handleDetail(record)">详情</a>
            <a-divider v-if="record.actStatus != '撤回'" type="vertical" />
            <a v-if="record.actStatus != '撤回'" @click="handleStop(record)">取消申请</a>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[2]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
      <a-tab-pane key="approved" tab="已审批">
        <div
          class="table-page-search-wrapper"
          style="display: flex; align-items: center; padding-top: 10px; padding-bottom: 24px; gap: 16px"
        >
          <a-form-item label="制单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date
              placeholder="请选择开始日期"
              class="query-group-cust"
              v-model="queryParamApproved.createTime_begin"
            ></j-date>
            <span class="query-group-split-cust"></span>
            <j-date
              placeholder="请选择结束日期"
              class="query-group-cust"
              v-model="queryParamApproved.createTime_end"
            ></j-date>
          </a-form-item>
          <a-button type="primary" @click="loadApprovedData" icon="search" style="margin-top: -3px">查询</a-button>
        </div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="approvedTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="approvedTable.dataSource"
          :pagination="{
            ...approvedTable.ipagination,
            onChange: handleApprovedPageChange, // 监听页码变化
            onShowSizeChange: handleApprovedPageSizeChange, // 监听每页数量变化
          }"
          :loading="approvedTable.loading"
          :rowSelection="{
            selectedRowKeys: approvedTable.selectedRowKeys,
            onChange: onScheduleSelectChangeApproved,
            type: 'radio',
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
            <a-divider type="vertical" />
            <a @click="handleDetail(record)">详情</a>
            <a-divider type="vertical" />
            <a v-if="record.isClose == 1" @click="OpenOrClose(record)">开启</a>
            <a v-if="record.isClose != 1" @click="OpenOrClose(record)">关闭</a>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[3]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
    </a-tabs>
    <tsl-pro-schedule-modal ref="modalForm" @ok="modalFormOk" />
    <tsl-production-plan-modal ref="modalPlan" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TslProScheduleModal from './modules/TslProScheduleModal'
import TslProductionPlanModal from './modules/TslProductionPlanModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import { getAction } from '@api/manage'
import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import { deepClone, getDate } from '../../utils'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { postAction, deleteAction } from '@api/manage'
import { stopProcess } from '@views/flowable/api/process'

export default {
  name: 'TslProScheduleList',
  mixins: [JeecgListMixin, JVxeTableModelMixin],
  components: {
    TslProScheduleModal,
    ActApplyBtn,
    ActHistoricDetailBtn,
    TslProductionPlanModal,
  },
  data() {
    return {
      refKeys: ['tempSaveDetail', 'needEditDetail', 'approvingDetail', 'approvedDetail'],
      detailTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '订单编号',
            key: 'orderCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 180,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '客户物料编码',
            key: 'customCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 180,
          },
          {
            title: '物料编码',
            key: 'productCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 180,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '物料名称',
            key: 'productBatchNo',
            type: JVXETypes.normal,
            align: 'center',
            width: 350,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '规格型号',
            key: 'materialSpec',
            dataIndex: 'materialSpec',
            type: JVXETypes.normal,
            align: 'center',
            width: 100,
          },
          {
            title: '主单位',
            key: 'meUnit',
            dataIndex: 'meUnit',
            dictCode: 'ji_unit',
            type: JVXETypes.select,
            align: 'center',
            width: 100,
            disabled: true,
          },
          {
            title: '数量',
            key: 'planQuantity',
            dataIndex: 'planQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '配方号',
            key: 'formulaNumber',
            dataIndex: 'formulaNumber',
            align: 'center',
            width: 150,
            disabled: true,
          },
          {
            title: '配方编码',
            key: 'formulaCode',
            dataIndex: 'formulaCode',
            align: 'center',
            width: 200,
          },
        ],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
        style: 'width: 28%;',
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      productType: 1,
      orderCode: '',
      description: 'TSL生产调度工作令管理页面',
      serviceName: 'tslProScheduleService',
      activeKey: 'notGenerated',
      dataColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '营销生产计划单',
          align: 'center',
          dataIndex: 'planCode',
          sorter: (a, b) => a.planCode.localeCompare(b.planCode, 'en'),
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderCode',
          sorter: (a, b) => a.orderCode.localeCompare(b.orderCode, 'en'),
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customCode',
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'productCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'productBatchNo',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'materialSpec',
        },
        {
          title: '配方号',
          align: 'center',
          dataIndex: 'formulaNumber',
        },
        {
          title: '主单位',
          align: 'center',
          dataIndex: 'meUnit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'planQuantity',
        },
        {
          title: '交货期',
          align: 'center',
          dataIndex: 'deliveryTime',
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'specialRemark',
        },
      ],
      planColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '营销生产计划单',
          align: 'center',
          dataIndex: 'planCode',
          sorter: (a, b) => a.planCode.localeCompare(b.planCode, 'en'),
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'typeName',
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customCode',
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'productCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'productBatchNo',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'materialSpec',
        },
        {
          title: '配方号',
          align: 'center',
          dataIndex: 'formulaNumber',
        },
        {
          title: '主单位',
          align: 'center',
          dataIndex: 'meUnit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'planQuantity',
        },
        {
          title: '交货期',
          align: 'center',
          dataIndex: 'deliveryTime',
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 110,
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '调度单号',
          align: 'center',
          dataIndex: 'code',
          sorter: (a, b) => a.code.localeCompare(b.code, 'en'),
        },
        {
          title: '调度开始',
          align: 'center',
          dataIndex: 'beginDate',
        },
        {
          title: '调度截止',
          align: 'center',
          dataIndex: 'endDate',
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },
        {
          title: '流程状态',
          align: 'center',
          dataIndex: 'actStatus',
        },
        {
          title: '待处理节点',
          align: 'center',
          dataIndex: 'taskName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      approvedTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      approvingTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      needEditTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      tempSaveTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      url: {
        waitGenerateList: '/pro/tslProSchedule/waitGenerateList',
        waitGeneratePlan: '/pro/tslProSchedule/waitGeneratePlan',
        list: '/pro/tslProSchedule/list',
        close: '/pro/tslProSchedule/close',
        delete: '/pro/tslProSchedule/delete',
        deleteBatch: '/pro/tslProSchedule/deleteBatch',
        exportXlsUrl: '/pro/tslProSchedule/exportXls',
        importExcelUrl: 'pro/tslProSchedule/importExcel',
        deletePlan: '/pro/tslProSchedule/deletePlan',
        tslProScheduleDetail: {
          list: '/pro/tslProSchedule/queryTslProScheduleDetailByMainId',
        },
      },
      queryParamApproved: {
        createTime_begin: undefined,
        createTime_end: undefined,
        order: 'create_by desc',
        salesType: 1,
        status: 999,
      },
      settingColumns: [],
    }
  },
  created() {
    this.queryParamApproved.createTime_begin = getDate('day', -7)
    this.queryParamApproved.createTime_end = getDate('day', 0)
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    rowSelect(record, index) {
      return {
        on: {
          click: (event) => {
            let params = { id: record.id }
            this.loadDetailData(params)
          },
        },
      }
    },
    // 暂存中页：处理页码变化
    handleTempSavePageChange(page, pageSize) {
      this.tempSaveTable.ipagination.current = page
      this.tempSaveTable.ipagination.pageSize = pageSize
      this.loadTempSaveData()
    },
    // 暂存中页：处理每页数量变化
    handleTempSavePageSizeChange(page, pageSize) {
      this.tempSaveTable.ipagination.current = page
      this.tempSaveTable.ipagination.pageSize = pageSize
      this.loadTempSaveData()
    },
    // 暂存中页：处理页码变化
    handleNeedEditPageChange(page, pageSize) {
      this.needEditTable.ipagination.current = page
      this.needEditTable.ipagination.pageSize = pageSize
      this.loadNeedEditData()
    },
    // 暂存中页：处理每页数量变化
    handleNeedEditPageSizeChange(page, pageSize) {
      this.needEditTable.ipagination.current = page
      this.needEditTable.ipagination.pageSize = pageSize
      this.loadNeedEditData()
    },
    // 暂存中页：处理页码变化
    handleApprovingPageChange(page, pageSize) {
      this.approvingTable.ipagination.current = page
      this.approvingTable.ipagination.pageSize = pageSize
      this.loadApprovingData()
    },
    // 暂存中页：处理每页数量变化
    handleApprovingPageSizeChange(page, pageSize) {
      this.approvingTable.ipagination.current = page
      this.approvingTable.ipagination.pageSize = pageSize
      this.loadApprovingData()
    },
    // 已审批页：处理页码变化
    handleApprovedPageChange(page, pageSize) {
      this.approvedTable.ipagination.current = page
      this.approvedTable.ipagination.pageSize = pageSize
      this.loadApprovedData()
    },
    // 已审批页：处理每页数量变化
    handleApprovedPageSizeChange(page, pageSize) {
      this.approvedTable.ipagination.current = page
      this.approvedTable.ipagination.pageSize = pageSize
      this.loadApprovedData()
    },
    onScheduleSelectChangeTempSave(selectedRowKeys, selectedRows) {
      this.tempSaveTable.selectedRowKeys = selectedRowKeys
      let params = { id: selectedRowKeys[0] }
      this.loadDetailData(params)
    },
    onScheduleSelectChangeNeedEdit(selectedRowKeys, selectedRows) {
      this.needEditTable.selectedRowKeys = selectedRowKeys
      let params = { id: selectedRowKeys[0] }
      this.loadDetailData(params)
    },
    onScheduleSelectChangeApproved(selectedRowKeys, selectedRows) {
      this.approvedTable.selectedRowKeys = selectedRowKeys
      let params = { id: selectedRowKeys[0] }
      this.loadDetailData(params)
    },
    onScheduleSelectChangeApproving(selectedRowKeys, selectedRows) {
      this.approvingTable.selectedRowKeys = selectedRowKeys
      let params = { id: selectedRowKeys[0] }
      this.loadDetailData(params)
    },
    loadDetailData(params) {
      this.detailTable.dataSource = []
      this.detailTable.loading = true
      getAction(this.url.tslProScheduleDetail.list, params).then((res) => {
        this.detailTable.loading = false
        if (res.success) {
          this.detailTable.dataSource = res.result
        }
      })
    },
    addSchedule(rows) {
      if (!this.selectedRowKeys || this.selectedRowKeys.length == 0) {
        this.$message.error('请至少选择一条营销生产计划！')
        return
      }
      if (this.productType == 3) {
        let sels = []
        this.dataSource.forEach((element) => {
          if (this.selectedRowKeys.includes(element.id)) {
            sels.push(element)
          }
        })
        this.$refs.modalForm.add({ ids: sels, productType: this.productType, salesType: 1 })
      } else {
        this.$refs.modalForm.add({ ids: this.selectedRowKeys, productType: this.productType, salesType: 1 })
      }
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
    },
    addPlan() {
      this.$refs.modalPlan.add({})
      this.$refs.modalPlan.title = '新增'
      this.$refs.modalPlan.disableSubmit = false
    },
    handleEditPlan(record) {
      this.$refs.modalPlan.edit(record)
      this.$refs.modalPlan.title = '编辑'
      this.$refs.modalPlan.disableSubmit = false
    },
    async handleDeletePlan(id) {
      deleteAction(this.url.deletePlan, { id: id }).then((res) => {
        if (res.success) {
          this.loadNotGeneratedData()
        }
      })
    },
    loadNotGeneratedData() {
      this.selectedRowKeys = []
      this.dataSource = []
      this.loading = true
      if (this.productType == 3) {
        getAction(this.url.waitGeneratePlan, {})
          .then((res) => {
            if (res.success) {
              this.dataSource = res.result.records || res.result
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
      } else {
        let param = {
          productType: this.productType,
          salesType: '1',
        }
        getAction(this.url.waitGenerateList, param)
          .then((res) => {
            if (res.success) {
              this.dataSource = res.result.records || res.result
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
    },
    loadTempSaveData() {
      this.tempSaveTable.loading = true
      this.queryParam.status = -2
      this.queryParam.salesType = 1
      this.queryParam.pageNo = this.tempSaveTable.ipagination.current
      this.queryParam.pageSize = this.tempSaveTable.ipagination.pageSize
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.tempSaveTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.tempSaveTable.ipagination.total = res.result.total
            } else {
              this.tempSaveTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.tempSaveTable.loading = false
        })
    },
    loadNeedEditData() {
      this.needEditTable.loading = true
      this.queryParam.status = -1
      this.queryParam.salesType = 1
      this.queryParam.pageNo = this.needEditTable.ipagination.current
      this.queryParam.pageSize = this.needEditTable.ipagination.pageSize
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.needEditTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.needEditTable.ipagination.total = res.result.total
            } else {
              this.needEditTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.needEditTable.loading = false
        })
    },
    loadApprovingData() {
      this.approvingTable.loading = true
      this.queryParam.status = 0
      this.queryParam.salesType = 1
      this.queryParam.pageNo = this.approvingTable.ipagination.current
      this.queryParam.pageSize = this.approvingTable.ipagination.pageSize
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.approvingTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvingTable.ipagination.total = res.result.total
            } else {
              this.approvingTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvingTable.loading = false
        })
    },
    loadApprovedData() {
      this.approvedTable.loading = true
      this.queryParamApproved.pageNo = this.approvedTable.ipagination.current
      this.queryParamApproved.pageSize = this.approvedTable.ipagination.pageSize
      getAction(this.url.list, this.queryParamApproved)
        .then((res) => {
          if (res.success) {
            this.approvedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvedTable.ipagination.total = res.result.total
            } else {
              this.approvedTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvedTable.loading = false
        })
    },
    async OpenOrClose(record) {
      record.isClose = record.isClose == 1 ? 0 : 1
      await postAction(this.url.close, record)
      this.loadApprovedData()
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.handleChangeTab()
    },
    handleChangeTab() {
      this.detailTable.dataSource = []
      switch (this.activeKey) {
        case 'notGenerated':
          this.loadNotGeneratedData()
          break
        case 'tempSave':
          this.loadTempSaveData()
          break
        case 'needEdit':
          this.loadNeedEditData()
          break
        case 'approving':
          this.loadApprovingData()
          break
        case 'approved':
          this.loadApprovedData()
          console.log('approved')
          break
        case 'closed':
          this.loadClosedData()
          console.log('closed')
          break
        default:
          break
      }
    },
    handleStop(row) {
      const params = {
        instanceId: row.processInstanceId,
      }
      stopProcess(params).then((res) => {
        this.$message.success(res.message)
        this.loadData()
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
::v-deep .ant-table-tbody tr {
  height: 36px !important;
}
</style>
